#! /usr/bin/env python
# -*- coding: utf-8 -*-
# 文件大小排序

import  os
# with os.open("计算机\Redmi 3\内部存储设备","r") as file:
#     print(file)

import os


def getFileFolderSize(fileOrFolderPath):
    """
    get size for file or folder
    # source:https://github.com/crifan/crifanLibPython/blob/master/crifanLib/crifanFile.py
    """
    totalSize = 0

    if not os.path.exists(fileOrFolderPath):
        return totalSize

    if os.path.isfile(fileOrFolderPath):
        totalSize = os.path.getsize(fileOrFolderPath)  # 5041481
        return totalSize

    if os.path.isdir(fileOrFolderPath):
        with os.scandir(fileOrFolderPath) as dirEntryList:
            for curSubEntry in dirEntryList:
                curSubEntryFullPath = os.path.join(fileOrFolderPath, curSubEntry.name)
                if curSubEntry.is_dir():
                    curSubFolderSize = getFileFolderSize(curSubEntryFullPath)  # 5800007
                    totalSize += curSubFolderSize
                elif curSubEntry.is_file():
                    curSubFileSize = os.path.getsize(curSubEntryFullPath)  # 1891
                    totalSize += curSubFileSize

            return totalSize


normalFile = "F:\order"
normalFileSize = getFileFolderSize(normalFile)
print("normalFileSize=%s" % normalFileSize)
#
# userFolder = "/aaa/ddd/"
# userFolderSize = getFileFolderSize(userFolder)
# print("userFolderSize=%s" % userFolderSize)

